package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.category

import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.category.bean.ClassifyMenuBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class CategoryMenuFragment : SimpleListFragment<ClassifyMenuBean>() {

    companion object {
        fun newInstance(): CategoryMenuFragment {
            val args = Bundle()
            val fragment = CategoryMenuFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var onMenuItemClickListener: OnMenuItemClickListener? = null


    fun setOnMenuItemClickListener(onMenuItemClickListener: OnMenuItemClickListener?) {
        this.onMenuItemClickListener = onMenuItemClickListener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isAlwaysRefresh = false
        isInitialRefresh = false
        isAddDividerItemDecoration = false
        isEnableLoadMore = false
        delayedTime = 0
    }

    private var currentSelected = 0

    override fun getItemLayout() = R.layout.classify_menu_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: ClassifyMenuBean?) {

        val flag = helper?.layoutPosition == currentSelected

        helper?.setText(R.id.title_text_view, item?.catName)
            ?.setViewSelect(R.id.title_text_view, flag)
            ?.setViewSelect(R.id.indicator, flag)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(ClassifyMenuBean::class.java).endSubType().build()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        currentSelected = position
        mAdapter.notifyDataSetChanged()

        onMenuItemClickListener?.onItemClick(mAdapter.getItem(currentSelected))
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.classify_menu)
//        parseDate(jsonFromRaw)
//        onMenuItemClickListener?.onItemClick(mAdapter.getItem(currentSelected))

        RestClient.builder()
            .url(R.string.queryCats)
            .success {

                parseDate(it)

                onMenuItemClickListener?.onItemClick(mAdapter.getItem(currentSelected))

            }.failure {
                onFailed()
            }.error { _, _ ->
                onFailed()
            }
            .build()
            .post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val any = obj["firstLevelCats"]
//        Log.d("netRequest", "netRequest: ${any.toString()}")
        val ls: List<ClassifyMenuBean> =
            JSON.parseArray(any.toString(), ClassifyMenuBean::class.java)
        loadListDate(ls)
    }

    interface OnMenuItemClickListener {
        fun onItemClick(menuBean: ClassifyMenuBean)

    }
}